package www.sofn.com.integral;

import cn.hutool.core.util.ObjectUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.sofn.com.accounts.AccountsService;
import www.sofn.com.accounts.domain.vo.WeChartMemberInfoVO;
import www.sofn.com.integral.domain.pojo.IntegralPO;
import www.sofn.com.integral.domain.query.IntegralQuery;
import www.sofn.com.integral.domain.vo.IncomeDataVO;
import www.sofn.com.integral.domain.vo.IncomeInfoVO;
import www.sofn.com.member.MemberService;
import www.sofn.com.relation.MemberRelationMapper;
import www.sofn.com.relation.domain.pojo.MemberRelationPO;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class IntegralService {

    @Resource
    private IntegralMapper mapper;

    @Resource
    private MemberRelationMapper memberRelationMapper;

    @Resource
    private MemberService memberService;

    @Resource
    private AccountsService accountsService;
    /**
     * 积分分配规则  1、自己分配 20% 上级分配 10%
     * @param totalIntegral
     * @param memberId
     */
    @Transactional
    public void allocationIntegral(BigDecimal totalIntegral,String memberId,Integer type){
        //给自己分配
        BigDecimal myIncome=  totalIntegral.setScale(2, RoundingMode.FLOOR).multiply(new BigDecimal("0.2"));
        final IntegralPO myIntegralPO = new IntegralPO().setIncomeDate(LocalDateTime.now()).setIntegral(myIncome).setMemberId(Long.valueOf(memberId)).setIncomeType(type);
        mapper.insert(myIntegralPO);
        memberService.addMemberIntegral( myIncome,memberId);
        //给上级分配
        final MemberRelationPO memberRelationPO = memberRelationMapper.selectById(memberId);
        if(ObjectUtil.isNull(memberRelationPO)||  ObjectUtil.isNull(memberRelationPO.getSuperiorMemberId())) return;
        BigDecimal superiorIncome=  totalIntegral.setScale(2, RoundingMode.FLOOR).multiply(new BigDecimal("0.1"));
        final IntegralPO superiorIntegralPO = new IntegralPO().setIncomeDate(LocalDateTime.now()).setIntegral(superiorIncome).setMemberId(memberRelationPO.getSuperiorMemberId()).setIncomeType(type);
        mapper.insert(superiorIntegralPO);
        memberService.addMemberIntegral( superiorIncome,memberRelationPO.getSuperiorMemberId());
    }


    public List<IncomeInfoVO> finalIncome(IntegralQuery query) {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        return mapper.finalIncome(query,personal.getId());
    }

    public IncomeDataVO incomeInfo() {
        IncomeDataVO incomeDataVO=new IncomeDataVO();
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        final BigDecimal todayIncome = mapper.getIncomeData(true, personal.getId());
        incomeDataVO.setTodayIncome(ObjectUtil.isNull(todayIncome)?BigDecimal.ZERO.setScale(2,RoundingMode.FLOOR):todayIncome.setScale(2,RoundingMode.FLOOR));
        final BigDecimal  historyIncome= mapper.getIncomeData(false,personal.getId());
        incomeDataVO.setHistoryIncome( ObjectUtil.isNull(historyIncome)?BigDecimal.ZERO.setScale(2,RoundingMode.FLOOR):historyIncome.setScale(2,RoundingMode.FLOOR));
        incomeDataVO.setTotalIncome(incomeDataVO.getTodayIncome().add(incomeDataVO.getHistoryIncome()));
        return incomeDataVO;
    }
}
